package com.tc.zookeeper;

import com.alibaba.fastjson.JSON;
import com.tc.zookeeper.model.DomainsDataModel;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

public class ZKUtils {

	public static String resolveSystemProperty(String key) {
		try {
			String value = System.getProperty(key);//获取系统属性
			if (value == null) {
				value = System.getenv(key);//获取环境变量
			}
			if (value == null) {
				value = "";
			}
			return value;
		} catch (Throwable ex) {
			return "";
		}
	}

	public static Map<String, Object> propertiesToMap(String pro) {
		if (StringUtils.isBlank(pro)) {
			return new LinkedHashMap<String, Object>();
		}
		String[] strArray = pro.split("\n");
		Map<String, Object> result = new LinkedHashMap<String, Object>();
		if (strArray != null && strArray.length > 0) {
			for (String str : strArray) {
				result.put(StringUtils.substringBefore(str, "="), StringUtils.substringAfter(str, "="));
			}
		}

		return result;
	}

	public static Map<String, Object> jsonToMap(String pro) {
		if (StringUtils.isBlank(pro)) {
			return new LinkedHashMap<String, Object>();
		}

		DomainsDataModel domainsDataModel = JSON.parseObject(pro, DomainsDataModel.class);
		Map<String, Object> result = new LinkedHashMap<String, Object>();
		result.put(ZKConstants.ZKCONNECTION, domainsDataModel.getZkconnection());
		return result;
	}

	public static boolean isMatch(String patternPath, String requestPath) {
		ZKAntPathMatcher matcher = new ZKAntPathMatcher();
		return matcher.match(StringUtils.lowerCase(patternPath), StringUtils.lowerCase(requestPath));
	}
}
